nums = [1, 2, 3, 4, 5, 6, 7], k = 3
nums = [1, 2], k = 5

// var rotate = function (nums, k) {
//     let len = nums.length
//     while (len < k) {
//         k = k % len
//     }
//     if (k === 0) return nums

//     function reverse(start, end) {
//         while (start < end) {
//             [nums[start], nums[end]] = [nums[end], nums[start]]
//             start++
//             end--
//         }
//     }

//     reverse(0, len - 1)
//     reverse(0, k - 1)
//     reverse(k, len - 1)
// };

var rotate = function (nums, k) {
    let len = nums.length;
    while (len < k) {
        k = k % len;
    }
    if (len === 0) return nums;

    function reverse(start, end) {
        while (start < end) {
            [nums[start], nums[end]] = [nums[end], nums[start]];
            start++;
            end--;
        }
    }

    reverse(0, len - 1)
    reverse(0, k - 1)
    reverse(k, len - 1)
}
console.log(rotate(nums, k));
